import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:flutter_yn_goods/config/app_color.dart';
import 'package:flutter_yn_goods/config/app_translations.dart';
import 'package:get/get.dart';


class LoadingUtils {
  static setup() {
    EasyLoading.instance
      ..indicatorType = EasyLoadingIndicatorType.fadingCircle
      ..maskType = EasyLoadingMaskType.custom
      ..toastPosition = EasyLoadingToastPosition.top
      // ..animationStyle
      ..indicatorSize = 30.0
      ..radius = 16.0
      ..maskColor = Colors.transparent
      ..progressColor = Colors.white
      ..backgroundColor = AppColor.color282828
      ..boxShadow= []
      ..indicatorColor = Colors.white
      ..textColor = Colors.white
      ..userInteractions = false
      ..contentPadding = const EdgeInsets.all(20)
      ..textPadding = const EdgeInsets.all(20)
      ..textStyle = const TextStyle(
        color: Colors.white,
        fontSize: 14,
        fontWeight: FontWeight.w600,
      )
      ..loadingStyle = EasyLoadingStyle.custom
      ..dismissOnTap = false;
    EasyLoading.addStatusCallback((status) {
      if (status == EasyLoadingStatus.dismiss && isShowLoading) {
        isShowLoading = false;
      }
    });
  }

  static bool isShowLoading = false;

  static void showLoading({
    String? status,
    Widget? indicator,
    EasyLoadingMaskType? maskType,
    bool? dismissOnTap,
  }) {
    EasyLoading.instance
      ..contentPadding = const EdgeInsets.all(20)
      ..backgroundColor = AppColor.color282828
      ..radius = 12
      ..userInteractions = false;

    if (isShowLoading && indicator == null) {
      return;
    }

    isShowLoading = true;
    EasyLoading.show(
      status: status ?? LocaleKeys.loading.tr,
      indicator: indicator,
      maskType: maskType,
      dismissOnTap: dismissOnTap,
    );
  }

  static Future showToast(String? msg) async {
    if (msg != null && msg.isNotEmpty) {
      EasyLoading.instance
        ..contentPadding =
            const EdgeInsets.symmetric(horizontal: 24, vertical: 12)
        ..backgroundColor = AppColor.color282828.withOpacity(0.8) // 设置背景色为黑色
        ..userInteractions = true
        ..radius = 40
        ..textStyle = const TextStyle(
          color: Colors.white,
          // fontWeight: FontWeight.w600
        );

      await EasyLoading.showToast(msg);
    }
  }

  static Future<void> dismiss({
    bool animation = true,
  }) {
    return EasyLoading.dismiss(animation: animation);
  }
  
  static void showSuccess(String? message) {
    EasyLoading.instance
      ..contentPadding = const EdgeInsets.all(20)
      ..backgroundColor = AppColor.color282828
      ..radius = 12
      ..userInteractions = false;
      
    EasyLoading.showSuccess(message ?? LocaleKeys.loading_success.tr);
  }
  
  static void showError(String? message) {
    EasyLoading.instance
      ..contentPadding = const EdgeInsets.all(20)
      ..backgroundColor = AppColor.color282828
      ..radius = 12
      ..userInteractions = false;
      
    EasyLoading.showError(message ?? LocaleKeys.loading_failed.tr);
  }
  
  static void showInfo(String? message) {
    EasyLoading.instance
      ..contentPadding = const EdgeInsets.all(20)
      ..backgroundColor = AppColor.color282828
      ..radius = 12
      ..userInteractions = false;
      
    EasyLoading.showInfo(message ?? "");
  }
  
  static bool get isShow => EasyLoading.isShow;
}
//
